package UILayer;
import java.util.Iterator;
import CtrLayer.CustomerCtr;
import ModelLayer.Customer;

/**
 * Write a description of class CustomerUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class CustomerUI extends InOut
{
    private static CustomerCtr custCtr = new CustomerCtr();
    private static int idC = 1000;
    
    public CustomerUI()
    {
        
    }

    public void customerMenu()
    {
        String[] customerMenu = {"Customer MENU", "Create customer", "Retrieve customer information", "Update customer information", "Delete customer", "List customers"};
        printMenu(customerMenu);
    }
    
    public void start()
    {
        boolean exit = false;
        while(!exit)
        {
            erase(1);
            customerMenu();
            int choice = inputInt("Choose: ");
            erase(1);
            switch(choice)
            {
                case 1 : {createCustomer();}break;
                case 2 : {getCustomer();}break;
                case 3 : {updateCustomer();}break;
                case 4 : {deleteCustomer();}break;
                case 5 : {listCustomers();}break;
                case 6 : {exit=true;}break;
                
                default : {};break;
            }
            if(choice>=1 && choice<=5 && choice!=3) waitK();
        }
    }
    
    public void listCustomers()
    {
        Iterator it = custCtr.getCustomersIterator();
        int k = 0;
        print("########## Customer List ##########");
        while(it.hasNext())
        {
            Customer cust = (Customer)it.next();
            print(cust.getId()+" "+cust.getName());
            k++;
        }
        print("\n -> There are "+k+" customers.");
    }
    
    public void createCustomer()
    {
        idC++;
        String n = inputString("Enter customer's name: ");
        String a = inputString("Enter customer's address: ");
        String pc = inputString("Enter customer's postal code: ");
        String ph = inputString("Enter customer's phone number: ");
        String e = inputString("Enter customer's e-mail address: ");
        String c = inputString("Enter customer's CVR number: ");
        String t = inputString("Enter customer's type: ");
        custCtr.createCustomer(idC, n, a, pc, ph, e, c, t);
        print("Customer with id " + idC + " was created.");
    }
    
    public void getCustomer()
    {
        int id = inputInt("Enter customer's ID: ");
        try {
            print("######## CUSTOMER INFORMATION ########");
            print("ID: " + custCtr.getCustomer(id).getId());
            print("Name: " + custCtr.getCustomer(id).getName());
            print("Address: " + custCtr.getCustomer(id).getAddress());
            print("Postal Code: " + custCtr.getCustomer(id).getPostCode());
            print("Phone number: " + custCtr.getCustomer(id).getPhone());
            print("E-mail address: " + custCtr.getCustomer(id).getEmail());
            print("CVR Number: " + custCtr.getCustomer(id).getCvr());
            print("Type: " + custCtr.getCustomer(id).getType());
        }
        catch(Exception e) { print("There is no customer with given id"); }
    }
    
    public void updateCustomerMenu(int idC)
    {
        String[] updateCustMenu = {"Update Customer with ID "+idC, "Update name", "Update address", "Update postal code", "Update phone number", "Update email", "Update CVR", "Update type"};
        printMenu(updateCustMenu);
    }
    
    public void updateCustomer()
    {
        int id = inputInt("Enter customer's ID: ");
        if(id==-1) print(" -> Invalid customer ID.");
        else
        {
            boolean exit = false;
            while(!exit)
            {
                erase(1);
                updateCustomerMenu(id);
                int choice = inputInt("Choose: ");
                
                if(choice < 1 || choice > 7) exit = true;
                else
                {
                    String upInf = "New value: ";
                    updateCustomer(id, choice, upInf);
                    waitK();
                }
            }
        }
        
    }
    
    public void updateCustomer(int id, int opt, String upInf)
    {
        String newValue = inputString(upInf);
        if(custCtr.updateCustomer(id, newValue, opt)) print(" -> The customer was successfully updated.");
        else print(" -> Customer with id "+id+" does not exist.");
    }
    
    public void deleteCustomer()
    {
        int id = inputInt("Enter customer's ID: ");
        if(custCtr.deleteCustomer(id)) print(" -> The customer was successfully deleted.");
        else print(" -> Customer with id "+id+" does not exist.");
    }
}
